package com.lkd.controller;


import com.lkd.dto.LoginDto;
import com.lkd.result.Result;
import com.lkd.service.UserService;
import com.lkd.vo.LoginUserVo;
import lombok.extern.slf4j.Slf4j;
import main.java.com.lkd.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user-service/user")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto dto){
//        调用service进行登录
        LoginUserVo loginUserVo = userService.login(dto);
//        生成jwt的token，返回给前端
//        放入payload载荷中的自定义用户信息，不能有敏感数据
//        登录成功之后，创建token
        Map<String, Object> map = new HashMap<>();
        map.put("userId",loginUserVo.getUserId());
        map.put("userName",loginUserVo.getUserName());
        String token = JwtUtil.createJWT("admin",3000L,map);
//        返回结果，写回token
        return Result.success(token);
    }
}
